package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnAddDTO;
import com.cheer.dto.OaLearnPeriodAddDTO;
import com.cheer.dto.OaLearnPeriodUpdateDTO;
import com.cheer.dto.OaLearnUpdateDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaLeamPeriodsService;
import com.cheer.service.OaLearnService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/learn")
public class OaLearnController {

    @Autowired
    private OaLearnService oaLearnService;

    @Autowired
    private OaLeamPeriodsService oaLeamPeriodsService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @ApiOperation("添加学习")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaLearnAddDTO learnAddDTO) {
        oaLearnService.add(learnAddDTO);
        return Result.success();
    }

    @ApiOperation("查询学习季")
    @GetMapping("/period")
    public Result<List<SimpleObjectVO>> period() {
        return Result.success(oaLeamPeriodsService.period());
    }

    @ApiOperation("查询学习")
    @GetMapping("/list")
    public Result<Page<OaLearnListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                            Integer pid) {
        return Result.success(oaLearnService.list(pageNum, pageSize, pid, userInfoHolder.getAdmin().getPid()));
    }

    @ApiOperation("删除学习")
    @GetMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestParam Integer id) {
        oaLearnService.removeById(id);
        return Result.success();
    }

    @ApiOperation("查询学习详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaLearnInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaLearnService.info(id));
    }

    @ApiOperation("修改学习")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaLearnUpdateDTO learnUpdateDTO) {
        oaLearnService.update(learnUpdateDTO);
        return Result.success();
    }

    @ApiOperation("查询学习季期")
    @GetMapping("/all")
    public Result<List<SimpleObjectVO>> all() {
        return Result.success(oaLearnService.all());
    }

    @ApiOperation("查询学习季期")
    @GetMapping("/sub")
    public Result<List<SimpleObjectVO>> sub(@RequestParam Integer pid) {
        return Result.success(oaLearnService.sub(pid));
    }
}
